﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateShopServiceCommand : ProviderQueueCommand<IProviderFactory, ShopInfoRecord, ResponseRecord>
    {
        public UpdateShopServiceCommand(IProviderFactory provider) : base(provider) { }

        protected override void Validate(ShopInfoRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
            if (record.District != null && !DistrictIdentifier.IsValid(record.District.DistrictIdentifier))
            {
                throw new ArgumentException("Identifier is invalidate", "request");
            }
            if (record.Province != null && !ProvinceIdentifier.IsValid(record.Province.Identifier))
            {
                throw new ArgumentException("ProvinceIdentifier is invalidate", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var shop = Record;
            var obj = new ShopInf()
            {
                Address = shop.Address,
                AreaCode = shop.AreaCode,
                BilltoCode = shop.BillToCode,
                CellNo1 = shop.CellNo1,
                CellNo2 = shop.CellNo2,
                Channel = shop.Channel,
                CreateDate = DateTime.Now,
                CreateUser = CONST_NAME.SYNCHRONIZE_ACCOUNT,
                CustomerCode = shop.CustomerCode,
                DistCode = (shop.District == null || !DistrictIdentifier.IsValid(shop.District.DistrictIdentifier)) ? null : new DistrictIdentifier(shop.District.DistrictIdentifier).PersistableID.ToString(),
                IsdnNo = shop.ISDNNo,
                OwnerEmail = shop.OwnerEmail,
                OwnerName = shop.OwnerName,
                ProvinceCode = (shop.Province == null || !ProvinceIdentifier.IsValid(shop.Province.Identifier)) ? null : new ProvinceIdentifier(shop.Province.Identifier).PersistableID.ToString(),
                RegId = shop.Region == null ? null : shop.Region.RegionIdentifier,
                ShopCode = shop.ShopCode,
                ShopName = shop.ShopName,
                ShopType = shop.ShopType,
                SoTransferFlag = BooleanString.TRUE,
                SoTransferDate = DateTime.Now,
                UpdateDate = DateTime.Now,
                UpdateUser = CONST_NAME.SYNCHRONIZE_ACCOUNT,
                UseFlag = shop.Active.ToBooleanString(),
            };
            var itemCheck = Providers.ShopInfo.RetrieveShopInf(Record.ShopCode);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.ShopInfo.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.ShopInfo.Update(obj);
            }
            return true;
        }
    }
}
